//
//  XCYInstanceTool.h
//  单例模式
//
//  Created by 冯华强 on 16/02/23.
//  Copyright (c) 2016年 HQ. All rights reserved.
//

#ifndef _4______XCYInstanceTool_h
#define _4______XCYInstanceTool_h


//#代表是一个字符串  ##代表后面拼接的参数
#define XCYSharedInstanceH(name)  + (instancetype)shared##name;

#if __has_feature(objc_arc)

#define XCYSharedInstanceM(name) static id _instance;\
\
+ (instancetype)shared##name \
{\
if (_instance == nil) {\
@synchronized(self){\
if (_instance == nil) { \
_instance = [[self alloc] init];\
}\
}\
}\
return _instance;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
NSLog(@"ARC");\
if (_instance == nil) {\
@synchronized(self){\
if (_instance == nil) {\
_instance = [super allocWithZone:zone];\
}\
}\
}\
return _instance;\
}\
\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance; \
}

#else

#define XCYSharedInstanceM(name) static id _instance;\
\
+ (instancetype)shared##name \
{\
if (_instance == nil) {\
@synchronized(self){\
if (_instance == nil) { \
_instance = [[self alloc] init];\
}\
}\
}\
return _instance;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
NSLog(@"MRC");\
if (_instance == nil) {\
@synchronized(self){\
if (_instance == nil) {\
_instance = [super allocWithZone:zone];\
}\
}\
}\
return _instance;\
}\
\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance; \
}\
\
- (instancetype)retain{ return _instance; } \
- (NSUInteger)retainCount{ return 1;} \
- (oneway void)release{ } \
- (instancetype)autorelease{ return _instance;}

#endif

#endif
